package com.hsy.linemoney.modules.auth.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageButton;

import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.RegistrationService;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.modules.common.EditTextExt;
import com.hsy.linemoney.utils.CommonUtils;
import com.liferay.mobile.android.exception.ServerException;

public class UpdateEmailAddressActivity extends CommonBaseActivityWithSimpleActionBar {


    protected EditTextExt emailAddress;
    protected EditTextExt emailAddressAgain;

    @Override
    public int getRightbuttonTextResId() {
        return R.string.txt_modify;
    }

    @Override
    public boolean onActionBarRightButtonClicked(ImageButton rightButton) {
        if (TextUtils.isEmpty(emailAddress.getText()) || TextUtils.isEmpty(emailAddressAgain.getText())) {
            showToast(R.string.txt_email_cannot_be_empty);
            if (TextUtils.isEmpty(emailAddress.getText())) {
                emailAddress.requestFocus();
            } else if (TextUtils.isEmpty(emailAddressAgain.getText())) {
                emailAddress.requestFocus();
            }
            return false;
        }
        if (!emailAddress.getText().toString().equals(emailAddressAgain.getText().toString())) {
            showToast(R.string.txt_email_not_same);
            return false;
        }
        showProgressDialog(UpdateEmailAddressActivity.this);
        RegistrationService service = new RegistrationService();
        service.updateEmailAddress(emailAddress.getText().toString(), new Callback<RegistrationService.UpdateEmailAddressResult>() {
            @Override
            public void onSuccess(int what, RegistrationService.UpdateEmailAddressResult result) {
                showToast(R.string.txt_email_modified);
                hideProgressDialog();
                finish();
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof ServerException) {
                    showToast(e.getMessage());
                } else {
                    showToast(R.string.txt_email_not_modified);
                }
                hideProgressDialog();
            }
        });
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_update_email);
        setTitle(R.string.txt_update_email);
        setRightButtonTextColor(R.color.white);
        initView();
    }

    private void initView() {
        emailAddress = (EditTextExt) findViewById(R.id.email_address);
        emailAddressAgain = (EditTextExt) findViewById(R.id.email_address_again);
    }
}
